package com.sentrix.auto.printercenter.infrastructure.persistence.impl;

import com.sentrix.auto.printercenter.infrastructure.persistence.dao.mapper.PrinterMapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import com.sentrix.auto.printercenter.infrastructure.persistence.IPrinterRepository;
import com.sentrix.auto.printercenter.domain.entity.PrinterEntity;

import java.util.List;

@Slf4j
@Component
public class PrinterRepositoryImpl extends ServiceImpl<PrinterMapper, PrinterEntity> implements IPrinterRepository {
    @Override
    public List<PrinterEntity> findByAgentCode(String code) {
        if (StringUtils.isNotBlank(code)) {
            return this.list(query().eq(PrinterEntity::getAgentCode, code).orderBy(PrinterEntity::getId, Boolean.FALSE));
        } else {
            return this.list(query().orderBy(PrinterEntity::getId, Boolean.FALSE));
        }
    }

    @Override
    public PrinterEntity findByCode(String printerCode) {
        return this.getOne(query().eq(PrinterEntity::getCode, printerCode));
    }

    @Override
    public void removeByCode(String printerCode) {
        this.remove(query().eq(PrinterEntity::getCode, printerCode));
    }

    @Override
    public PrinterEntity findByAgentCodeAndPrinterName(String agentCode, String printerName) {
        return this.getOne(query().eq(PrinterEntity::getAgentCode, agentCode).eq(PrinterEntity::getName, printerName));
    }
}
